package controller.filter;

import pojo.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/index.jsp")
public class FilterPower implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        处理响乱码
        response.setContentType("text/html;charset=utf-8");
//        获取session中保存的admin信息
        Admin admin = (Admin) request.getSession().getAttribute("admin");
//        b判断有木有用户信息
        if (admin != null) {
            filterChain.doFilter(request, response);
        } else {
            response.getWriter().print("<a href='/admin/login.jsp'>点击去登录</a>");
        }
    }
    @Override
    public void destroy() {

    }
}